package com.hexacta.hrs.sandbox.tasks2.actions;

import java.sql.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.hexacta.hrs.sandbox.tasks2.model.Activity;
import com.hexacta.hrs.sandbox.tasks2.model.Category;
import com.hexacta.hrs.sandbox.tasks2.model.User;
import com.hexacta.hrs.sandbox.tasks2.service.CategoryService;
import com.hexacta.hrs.sandbox.tasks2.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

@ContextConfiguration(locations = "/spring-orm-config.xml")
public class ActivityAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4001814270977370396L;
	@Autowired
	private UserService userService;

	private String id;
	private String newActivityName;
	private String selectedActivity;

	private Activity activity;

	private User user;

	private Date dummyDate = new Date(10);

	private String activityDuration = new String("0");

	@Autowired
	private CategoryService categoryService;

	/**
	 * agrega la actividad al usuario
	 * 
	 * @return
	 */
	public String newActivity() {

		this.setUser(this.getUserService().findById(new Long(this.getId())));

		this.getUserService().addActivity(this.getUser(),
				new Activity(this.getNewActivityName(), this.getDummyDate()));

		return SUCCESS;

	}

	/**
	 * obtiene la duracion de la actividad y la setea
	 * 
	 * @return
	 */
	public String calculateDuration() {

		this.setUser(this.getUserService().findById(new Long(this.getId())));

		this.setActivity(this.getUserService().findActivityById(new Long(this.getSelectedActivity())));

		this.setActivityDuration(Integer.toString(this.getActivity()
				.timeLength()));

		return SUCCESS;
	}
	
	/**
	 * solo setea la actividad seleccionada para luego ser manejada
	 * @return
	 */
	public String viewTasks(){
		
		this.setActivity(this.getUserService().findActivityById(new Long(this.getSelectedActivity())));
		
		return SUCCESS;
		
	}

	public List<Category> getAllCategories() {
		return this.getCategoryService().selectAllCategories();
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setNewActivityName(String newActivityName) {
		this.newActivityName = newActivityName;
	}

	public String getNewActivityName() {
		return newActivityName;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

	private Date getDummyDate() {
		return this.dummyDate;
	}

	public void setSelectedActivity(String selectedActivity) {
		this.selectedActivity = selectedActivity;
	}

	public String getSelectedActivity() {
		return selectedActivity;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public Activity getActivity() {
		return activity;
	}

	public void setActivityDuration(String activityDuration) {
		this.activityDuration = activityDuration;
	}

	public String getActivityDuration() {
		return activityDuration;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

}
